<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {
		buildSubFormData,
		subOnLoad,
		changeEditor,
		configSelectData,
		hasValue
	} from '../../../static/js/formUtil.js';
	import {
		ignoreHtml,
		ptFloat,
		minusF,
		getDateStr
	} from '@/static/js/common.js';
	import urlUtil from '@/static/js/urlUtil.js'
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				instCacheKey: "hr-kq-checkAttenceList-checkWay-inst-cache",
				posCacheKey: "hr-kq-checkAttenceList-checkWay-pos-cache",
				beforArr: [],
				beforList: []
			})
		},
		methods: {
			verifyPosId(){
				let _that = this
				let editor = _that.$refs.autoform.getEditor("checkWay","instId")
				if(!_that.$refs.autoform.formdata.instId){
					_that.om.toast("请先选择"+editor.title,3000)
					return false
				}else{
					return true
				}
			},
			changeBeforList(list){
				this.beforList = JSON.stringify(list)
			},
			
			getTimeNode(posId, attenceCode) {
				let _that = this
				_that.om.request(
					"tcheckattence/getAttenceNode", {
						code: "37348",
						posId: posId,
						attenceCode: attenceCode
					}
				).then(res => {
					if(_that.formdata.attenceCode === "37347"){
						_that.$refs.autoform.changeEditor("checkWay", "timeNodeCode","type","selectbox")
						_that.$refs.autoform.changeEditor("checkWay", "timeNodeCode", "data", configSelectData(res, 'code', "dicname"))
						_that.$refs.autoform.initSelectIndex("checkWay", "timeNodeCode")
					}else if(_that.formdata.attenceCode === "37285"){
						_that.$refs.autoform.changeEditor("checkWay", "timeNodeCode","type","labelbox")
					}else{
						_that.$refs.autoform.changeEditor("checkWay", "timeNodeCode","type","checkbox")
						_that.$refs.autoform.changeEditor("checkWay", "timeNodeCode", "data", configSelectData(res, 'code', "dicname",true))
					}
					if(_that.formdata.timeNodeCode){
					let editor = _that.$refs.autoform.getEditor("checkWay","timeNodeCode")
					for(let item of editor.data){
						if(_that.formdata.timeNodeCode.indexOf(item.value) != -1 && _that.formdata.attenceCode !== "37347"){
							_that.$refs.autoform.$set(item,"checked",true)
							}
						}
					}
				
				})
			},
			onSelectAttence() {
				let _that = this
				_that.getTimeNode(_that.$refs.autoform.formdata.posId, _that.$refs.autoform.formdata.attenceCode)
			},
			getAttence(id) {
				let _that = this
				_that.om.request(
					"tcheckattence/getAttence", {
						code: "37284",
						posId: id
					}
				).then(res => {
					_that.$refs.autoform.changeEditor("checkWay", "attenceCode", "data", configSelectData(res, "code", "dicname"))
					_that.$refs.autoform.initSelectIndex("checkWay","attenceCode")
				})
			},
			verifyPos(id) {
				let _that = this
				_that.om.request(
					"tcheckattence/verifyPos", {
						posId: id
					}
				).then(res => {
					_that.$refs.autoform.formdata.origAttenceCode = res.value.attenceCode
					_that.$refs.autoform.formdata.origAttenceName = res.value.attenceName
					_that.$refs.autoform.formdata.origTimeNodeCode = res.value.timeNodeCode
					_that.$refs.autoform.formdata.origTimeNodeName = res.value.timeNodeName
					
				})
			},
			getPosList(id) {
				let _that = this
				_that.$refs.autoform.changeEditor("checkWay", "posId", {
					link: "../../public/pageSearch/pageSearch",
					"linkParam": {
						other: JSON.stringify({
							instId: id
						}),
						namefield: "posname",
						url: "tposition/getPosCombobox",
						initload: true,
						searchfield: "q",
						cachename: _that.posCacheKey,
						prompt: "请输入岗位名称"
					}
				})
			}
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				uni.getStorage({
					key: _that.instCacheKey,
					success: (res) => {
						if (res.data) {
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.instId = d.id
							formdata.instName = d.instName
							_that.getPosList(d.id)
							_that.getAttence(d.id)
							uni.removeStorage({
								key: _that.instCacheKeyzu
							})
						}
					}
				})
				uni.getStorage({
					key: _that.posCacheKey,
					success: (res) => {
						if (res.data) {
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.posId = d.id
							formdata.posName = d.posname
							_that.verifyPos(d.id)
							uni.removeStorage({
								key: _that.posCacheKey
							})
						}
					}
				})
			}
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
				if (e.configDataCacheKey) {
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							if (res.data && Object.prototype.toString.call(res.data) === '[object Array]') {
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								changeEditor("checkWay", "instId", {
									link: "../../public/pageSearch/pageSearch",
									"linkParam": {
										other: JSON.stringify(),
										url: urlUtil.getInstList().url,
										namefield: urlUtil.getInstList().namefield,
										cachename: _that.instCacheKey,
										method: "post",
										searchfield: "instName",
										initload: true,
										prompt: urlUtil.getInstList().prompt
									}
								}, null, _that.autoformdata)
								if(_that.formdata.posId){
									_that.getAttence(_that.formdata.posId)
									_that.getTimeNode(_that.formdata.posId, _that.formdata.attenceCode)
								}
							}
						}
					})
				}
			})
		}
	}
</script>

<style>
</style>
